<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Carnew;
use App\Newcate;

class NewsController extends Controller
{
    //添加新闻栏目
    public function newcate(){
        if (empty($_POST)) {
             return view('admin.newcate');
        }else{
            $newcate = new Newcate();
            $newcate->name = $_POST['newcate'];
            return $newcate->save() ? redirect('newcate'):'失败';
        }
    }

    //新闻栏目列表
    public function newcatelist(){
        if (empty($_POST)) {
            $newcate = new Newcate();
            $newcates = $newcate::get();
            $news = new Carnew();
            $newnum=[];
            foreach ($newcates as $n) {
              $newnum[$n->id] = $news::where('cate_id',$n->id)->count();
            }
            //dd($newnum);
            return view('admin.newcatelist',['cates'=>$newcates,'newnum'=>$newnum]);
        }
    }

    //删除栏目
    public function newCatedel($id){
      $newcate = new Newcate();
      return $newcate->where('id',$id)->delete() ? redirect('newcatelist') :"BUG";
    }

    //编辑栏目
    public function newCatedit(Request $req , $id){
      if(empty($_POST)){
          $newcate = new Newcate();
          return view('admin.newcatedit',['row'=>$newcate::find($id)]);
      }
          $newcate = new Newcate();
          $newcate->name = $req->name;
          return $newcate->save()?redirect('newcatelist'):"BUG";
    }


    //新闻列表
    public function newlist($id = 0){
          $news = new Carnew();
          $newlist = $news::get();

          //dd($newlist);
        return view('admin.newlist',['newlist'=>$newlist]);
    }
      //编辑新闻
    public function editnew(Request $req, $xid){
          $news = new Carnew();
          $newcate = new Newcate();
          
        if (empty($_POST)) {
            $new = $news::where('xid',$xid)->first();
            $newcates = $newcate::get();
            $id =$new->cate_id;
            dd($id);
            // dd($new);
            $newcateone = $newcate::find($id);
            // dd($newcateone);
        return view('admin.editnew',['new'=>$new,'newcates'=>$newcates,'newcateone'=>$newcateone]);
        }else{
          $news = $news::find($xid);
          $news->title = $_POST['bname'];
          $news->cate= $_POST['cate'];
          $news->content = $_POST['content'];
          $news->pubtime = time();
          //dd("$req->file('pic')");
          $news->img = $req->file('pic')->move(public_path().'\newimages\\',mt_rand(10000,99999).'.jpg');
         return $news->save() ? redirect('newlist'):'失败';
        }    
    }

    //添加新闻
    public function addnew(){
        $newcate = new Newcate();
        $newcates = $newcate::get();
        return view('admin.addnew',['newcates'=>$newcates]);
    }
     public function postaddnew(Request $req){
        $news = new Carnew();

        $news->title = $_POST['bname'];
        $news->cate_id= $_POST['cate_id'];
        $news->content = $_POST['content'];
        $news->pubtime = time();
        $news->img = $req->file('pic')->move(public_path().'\newimages\\',mt_rand(10000,99999).'.jpg');
        return $news->save()?redirect('newlist'):'失败';   
    }
    //删除新闻
     public function delnew($xid){ 
        $news = new Carnew();
       // dd($xid);
        return $news::where('xid',$xid)->delete()? redirect('newlist'): '删除失败';
    }
}
